package com.zzty.daoimpl;


import com.zzty.common.DBUtilPool;
import com.zzty.dao.GroupDao;
import com.zzty.entity.Group;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class GroupDaoImpl implements GroupDao {

    public boolean insert(Group group) {
        boolean result=false;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("insert into groupp(userName1,userName2) values(?,?)");
            pst.setString(1,group.getUserName1());
            pst.setString(2,group.getUserName2());
            Integer count=0;
            count=pst.executeUpdate();
            if(count>0){
                result=true;
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Integer getIdByUserName(String userName) {
        Integer Id=null;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("select id from groupp where userName1=? or userName2=?");
            pst.setString(1,userName);
            pst.setString(2,userName);
            ResultSet rs=pst.executeQuery();
            while(rs.next()){
                Id=rs.getInt("id");
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Id;
    }
    public String getUserNamesByUserName(String userName){
        String userNames=null;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("select * from groupp where userName1=? or userName2=?");
            pst.setString(1,userName);
            pst.setString(2,userName);
            ResultSet rs=pst.executeQuery();
            while (rs.next()){
                userNames=rs.getString("userName1")+"_"+rs.getString("userName2");
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userNames;
    }
    public String getUserNamesById(Integer id){
        String userNames=null;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("select * from groupp where id=?");
            pst.setInt(1,id);
            ResultSet rs=pst.executeQuery();
            while (rs.next()){
                userNames=rs.getString("userName1")+"_"+rs.getString("userName2");
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userNames;
    }


    public String getName1ById(Integer id){
        String name1=null;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("select userName1 from groupp where id=?");
            pst.setInt(1,id);
            ResultSet rs=pst.executeQuery();
            while(rs.next()){
                name1=rs.getString("userName1");
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return name1;
    }
    public String getName2ById(Integer id){
        String name2=null;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("select userName2 from groupp where id=?");
            pst.setInt(1,id);
            ResultSet rs=pst.executeQuery();
            while(rs.next()){
                name2=rs.getString("userName2");
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return name2;
    }

    @Override
    public Integer checkGrouped(String userName) {
        Integer count=0;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("select count(*) as count from groupp where userName1=? or userName2=?");
            pst.setString(1,userName);
            pst.setString(2,userName);
            ResultSet rs=pst.executeQuery();
            while (rs.next()){
                count=rs.getInt("count");
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return count;
    }

    @Override
    public List getGroups() {
        List<Group> groups=null;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("select * from groupp");
            ResultSet rs=pst.executeQuery();
            while(rs.next()){
                if(groups==null){
                    groups=new ArrayList<Group>();
                }
                Group group=new Group();
                group.setId(rs.getInt("id"));
                group.setUserName1(rs.getString("userName1"));
                group.setUserName2(rs.getString("userName2"));
                groups.add(group);
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return groups;
    }
    public boolean deletes(Integer groupId){
        boolean result=false;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("delete from groupp where id=?");
            pst.setInt(1,groupId);
            Integer count=0;
            count=pst.executeUpdate();
            if(count>0){
                result=true;
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
